package csvloader.tables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.db.BeanBase;
import com.db.Table;

import csvloader.beans.M_CategoryMappingBean;

public class M_CategoryMappingTable extends Table {

	public static final String TABLE_NAME = "M_CATEGORY_MAPPING";
	public static final String COL_WEBSITE_CODE = "WEBSITE_CODE";
	public static final String COL_SRC_CATEGORY_NAME = "SRC_CATEGORY_NAME";
	public static final String COL_SRC_BELONG_CATEGORY_NAME = "SRC_BELONG_CATEGORY_NAME";
	public static final String COL_DELETE_FLAG = "DELETE_FLAG";
	public BeanBase[] selectAll(Connection conn) throws SQLException {
		final String sql = "SELECT " 
				+ COL_WEBSITE_CODE + ","
				+ COL_SRC_CATEGORY_NAME + "," 
				+ COL_SRC_BELONG_CATEGORY_NAME + ","
				+ COL_DELETE_FLAG 
				+ " FROM " + TABLE_NAME;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Vector<M_CategoryMappingBean> v = new Vector<M_CategoryMappingBean>();
		try {
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			int i = 1;
			while (rs.next()) {
				i = 1;
				M_CategoryMappingBean rec = new M_CategoryMappingBean();
				rec.setWebsiteCode(rs.getInt(i++));
				rec.setSrcCategoryName(rs.getString(i++));
				rec.setSrcBelongCategoryName(rs.getString(i++));
				rec.setDeleteFlag(rs.getInt(i++) == NOT_DELETE_FLAG);
				v.add(rec);
			}
		} finally {
			if (rs != null) {
				rs.close();
			}

			if (stmt != null) {
				stmt.close();
			}
		}
		return (M_CategoryMappingBean[]) v.toArray(new M_CategoryMappingBean[v.size()]);
	}

}
